<?php
/**
*   +----------------------------------------------------------------------
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------
*   | Creater Time : 2013-6-15
*   +----------------------------------------------------------------------
*   | Link ( http://www.phpyrb.com  http://www.cloudsskill.com )
*   +----------------------------------------------------------------------
**/

	class Articleb2b {
		protected $cates;
		
		function __construct() {
			$this->cates = array();
		}
		
		/**
		 * 返回分类下的文章ID
		 * @param int $_cateid
		 * @param int $_uid
		 * @return array $catrart
		 */
		function artlist ($_cateid,$_offset = '') {
			$model = M('ArticleB2b');
			$_cateid ? $where['catid'] = $_cateid : $this->_message(FALSE, '请传入分类ID！',__LINE__);
			$where['status'] = 1;
			$cateart = $model->field('id,title,catid,description,content,updatetime')->where($where)->order('updatetime DESC')->limit($_offset)->select();
// 			echo $model->getLastSql();
// 			dump($cateart);
// 			exit;
			return $cateart;
		}
		
		/**
		 * 返回分类下的文章ID
		 * @param int $_cateid
		 * @param int $_uid
		 * @return array $catrart
		 */
		function catetoart ($_cateid) {
			$model = M('ArticleB2b');
			$_cateid ? $where['catid'] = $_cateid : $this->_message(FALSE, '请传入分类ID！',__LINE__);
			$where['status'] = 1;
			$cateart = $model->field('id,title,catid,description,content,updatetime')->where($where)->order('id DESC')->find();
			// 			echo $model->getLastSql();
			// 			dump($cateart);
			// 			exit;
			return $cateart;
		}
		
		/**
		 * 返回分类下的文章ID
		 * @param int $_cateidasldjflajsflsjdkfjlsjdlfjlajfl
		 * @param int $_offset
		 * @return array $catrart,
		 */
		function catetoarts($_cateid,$_offset = 6){
			$model = M('ArticleB2b');
			$where['status'] = 1;
			$where['catid'] = is_array($_cateid) ? array('IN',implode(',', $_cateid)) : $_cateid;
			$cateart = $model->field('id,title,catid,updatetime')->where($where)->order('updatetime DESC')->limit($_offset)->select();
			// 			echo $model->getLastSql();
			// 			dump($cateart);
			// 			exit;
			return $cateart;
		}
		
		/**
		 * 返回用户的分类
		 * @param int $_uid
		 * @return array $cate
		 */
		function cate ($_type = '1') {
			$model = M('ProductCategoryTag');
			$_type ? $where['type'] = $_type : false;
			$cate = $model->where($where)->select();
// 			echo $model->getLastSql();
			return $cate;
		}

		/**
		 * 统计条数
		 * @param string $_model 表名
		 * @param string $_felid 字段
		 * @param array $_where 条件
		 * @return array $count
		 */
		function count($_model,$_where = array(),$_felid = 'id'){
			$model = M("$_model");
			$where = $_where;
			$count = $model->where($where)->count('id');
// 			echo $model->getLastSql();
			return $count;
		}
		
		/**
		 * 菜单列表
		 * @param int $catid
		 * @return array  $results
		 */
		function catestyle($_cate,$_id){
			$result = array();
			foreach ($_cate as $key=>$v){
				if($v['parent_id'] == $_id){
					$i['id'] = $v['id'];
					$i['name'] = $v['name'];
					$i['parent_id'] = $v['parent_id'];
					$i['content'] = $this->catestyle($_cate,$v['id']);
					$result[] = $i;
				}
			}
			return $result;
		
		}
		
		/**
		 * 返回标签子分类
		 * @param array $_tags
		 * @param array $_pid
		 * @param array $_level
		 * @return array $this->tree
		 */
		function catetochildren ($_cate,$_pid = 0,$_level = 1,$clist=array()) {
			foreach ($_cate as $k => $v){
				if($v['parent_id'] == $_pid){
					$v['level'] = $_level;
					$clist[$v['id']] = $v;
					$this->catetochildren($_cate, $v['id'],$_level+1,$clist);
				}
			}
			return $clist;
		}
		
		/**
		 * 返回树形菜单
		 * @param array $_cate
		 * @return array $_cate
		 */
		function catelevel ($_cate) {
			foreach ($_cate as $k => &$v){
				$stra = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|';
				$strb = '&nbsp;--&nbsp;';
				$strc = '';
				for($i=0;$i<$v['level'];$i++){
					$strc = $strc.$stra;
				}
				$v['name'] = $strc.$strb.$v['name'];
			}
			return $_cate;
		}
		
		/**
		 * 搜索 返回IDS
		 * @param srting $_keyword
		 * @return array $ids
		 */
		function search ($_keyword) {
			$model = M('ArticleB2b');
			$where['kword']  = array('like',"%$_keyword%");
			$where['title']  = array('like',"%$_keyword%");
			$where['content']  = array('like',"%$_keyword%");
			$where['_logic'] = 'or';
			$ids = $model->field('id')->where($where)->select();
			$ids = $this->_arr2to1($ids);
			return $ids;
		}
		 
		
		/**
		 * 根据ID返回文章内容
		 * @param string|array $_id
		 * @return array $artinfo
		 */
		function content ($_id ,$_field = '*') {
			$model = M('ArticleB2b');
			$where['id'] = $_id;
			$artinfo = $model->field($_field)->where($where)->find();
			return $artinfo;
		}
		 
		/**
		 * 更新点击率
		 * @param 文章ID $_artid
		 * @return array $reid
		 */
		function hots ($_artid) {
			$model = M('ArticleB2b');
			$where['id'] = $_artid;
			$reid = $model->where($where)->setInc('hots');
// 			echo $model->getLastSql();
			return $reid;
		}
		
		
		/**
		 * 文章下载
		 * @param $_path $_name
		 * @return
		 */
		public function downloads($_path,$_name){
			if (!file_exists($_path)){
				header("Content-type: text/html; charset=utf-8");
				echo "File not found!";
				exit;
			} else {
				$file = fopen($_path,"r");
				Header("Content-type: application/octet-stream");
				Header("Accept-Ranges: bytes");
				Header("Accept-Length: ".filesize($_path));
				Header("Content-Disposition: attachment; filename=$_name");
				echo fread($file, filesize($_path));
				fclose($file);
			}
		}
		
		/**
		 * 添加、修改
		 * @param array $_data 数据
		 * @param string $_model 模型
		 * @param string $_type 动作名
		 */
		function add_updata ($_data,$_model = 'ArticleB2b',$_type = 'add') {
			$model = M("$_model");
			if($_data){
				$reid = $model->create($_data);
				if($reid){
					if($_type == 'add'){
						$reid = $model->add();
						if($this->Debug) dump($model->getLastSql());
						$reid = $this->_message($reid, '添加失败！',__LINE__);
						return $reid;
					}elseif($_type == 'updata'){
						$reid = $model->save();
						if($this->Debug) dump($model->getLastSql());
						$reid = $this->_message($reid, '添加失败！',__LINE__);
						return $reid;
					}else{
						$this->_message(false, '请传入正确的动作！', __LINE__);
					}
				}else{
					$this->_message(false, "创建模型失败！", __LINE__);
				}
			}else{
				$this->_message(false, '没有数据！', __LINE__ );
			}
		}
		
		/**
		 * 根据ID删除数据
		 * @param string|array $_id
		 */
		function delect ($_id) {
			$model = M("ArticleB2b");
			if($_id){
				$where['id'] = is_array($_id) ? array('IN',implode(',', $_id)) : $_id;
				$reid = $model->where($where)->delete();
				$this->_message($reid, '删除失败！',__LINE__);
			}else{
				$this->_message(false, '请您要删除的ID！',__LINE__);
			}
		}
		
		/**
		 * 返回提示信息
		 * @param multitype $_val
		 * @param json
		 */
		function _message($_val,$_msg,$_line = false){
			if(!$_val || empty($_val) || $_val === 'false'){
				if($this->Debug){
					$errormsg = '错误提示：'.$_msg.'在第'."$_line".'行！';
					exit($errormsg);
				}else{
					return 'false';
					exit;
				}
			}else{
				return 'true';
				exit;
			}
		}
		
		/**
		 * 二维数组返回一维或字符串
		 * @param array $_arr
		 * @param string $_field
		 * @param string $_type 1为数组 2为字符串
		 * @return multitype:array|multitype:string
		 */
		function _arr2to1($_arr,$_field = 'id',$_type = '1'){
			if ( ! $_arr ) return array();
			$arrayids = array();
			foreach ($_arr as $k => $v){
				$arrayids[$k] = $v[$_field];
			}
			if($_type != '1') $arrayids = implode(',',$arrayids);
			return $arrayids;
		}
	}
